package com.jml.jdk8.方法引用;

import com.jml.jdk8.base.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @Author: J
 * @DATE: 2022-04-15 15:40
 */
public class MethodReference {
    public static void main(String[] args) {
    }

    public static void referceObject001() {
        List<Student001> students = new ArrayList<>();
        students.sort(Student001::compareScore);
    }

    public static void referceObject() {
        List<Student> students = getList();
        StudentComparator studentComparator = new StudentComparator();
        //使用lambda表达式进行排序
        students.sort((s1, s2) -> studentComparator.compareStudentByScore(s1, s2));
        //使用方法引用
        students.sort(studentComparator::compareStudentByScore);
        students.forEach(student -> System.out.println(student.getScore()));
    }

    public static void referceStatic() {
        List<Student> students = getList();
        //使用lambda表达式进行排序
        students.sort((s1, s2) -> {
            return Student.compareStudentByScore001(s1, s2);
        });
        //使用方法引用
        students.sort(Student::compareStudentByScore001);
        //使用lambda表达式显示结果
        students.forEach(student -> System.out.println(student.getScore()));
        //使用方法引用显示结果
        students.forEach(System.out::println);
    }

    public static List<Student> getList() {
        Student zs = new Student("zs", 10);
        Student ls = new Student("ls", 40);
        Student ww = new Student("ww", 50);
        Student zl = new Student("zl", 90);
        List<Student> students = new ArrayList<>();
        students.add(zs);
        students.add(zl);
        students.add(ls);
        students.add(ww);
        return students;
    }


    public void aa() {
        Function<Student, String> function = Student::getName;
        BiConsumer<Student, String> biConsumer = Student::setName;
        Student s1 = new Student();
        //因为biConsumer接收两个参数没有返回，
        //相当于s1去调用setName方法，nnnn是作为参数
        setName(s1, "nnnn", Student::setName);
        //因为function接收一个参数返回一个参数，
        //相当于s1去调用getName方法
        getName(s1, Student::getName);
        System.out.println(s1);
    }

    public static void setName(Student s1, String str,
                               BiConsumer<Student, String> biConsumer) {
        biConsumer.accept(s1, str);
    }

    public static void getName(Student s1, Function<Student, String> function) {
        String name = function.apply(s1);
        System.out.println(name);
    }
}
